package app;

import java.util.ArrayList;

public class ReportManager {
	
	private ArrayList<search.WithinResult> withinResults;
	private ArrayList<search.BetweenResult> betweenResults;
	private ArrayList<Essay> essays;
	
	public ReportManager(ArrayList<Essay> essays, ArrayList<search.WithinResult> results, int high, int low)
	{
		this.essays = essays;
		this.withinResults = results; 
	}

	public ReportManager(ArrayList<Essay> essays, ArrayList<search.BetweenResult> results)
	{
		this.essays = essays;
		this.betweenResults = results;
	}
	
	/**
	 * Attempts to make sense of the results
	 * passed in as an ArrayList
	 */
	public void analyzeBetween()
	{
		for(int i=0;i<betweenResults.size();i++){
			search.BetweenResult result = (search.BetweenResult)betweenResults.get(i);
			if(!result.original){
				//The result was flagged
				System.out.print("The sentence '" + result.sentence + "'(" + result.thisSentNum + ") was flagged as matching sentence number " + result.sentNum + " in ");
				for(int j=0;j<essays.size();j++){
					Essay essay = (Essay)essays.get(j);
					if(essay.id == result.antId)
						System.out.print("'" + essay.subject + " by '" + essay.author + "'.\n");
				}
			}
		}
		for(int i=0;i<betweenResults.size();i++){
			search.BetweenResult result = (search.BetweenResult)betweenResults.get(i);
			if(result.original)
				System.out.println("The sentence '" + result.sentence + "'(" + result.thisSentNum + ") was flagged as original.");
		}
		System.out.println();
	}
	
	public void analyzeWithin()
	{
		//Calculate graph high
		search.WithinResult result;
		double sentHi=0, habHi = 0;
		for(int i=0;i<withinResults.size();i++){
			result = (search.WithinResult) withinResults.get(i);
			if(Math.abs(result.sentDev) > sentHi)
				sentHi = Math.abs(result.sentDev);
			if(Math.abs(result.habDev) > habHi)
				habHi = Math.abs(result.habDev);
		}
	
		//Scale results
		for(int i=0;i<withinResults.size();i++){
			result = (search.WithinResult)withinResults.get(i);
			result.sentDev = Math.round(150 / ((sentHi / result.sentDev)));
			result.habDev = Math.round(150 / ((habHi / result.habDev)));
			
			//Check plagiarised
			if(((result.sentDev - result.habDev) >= 20) || ((result.habDev - result.sentDev) >= 20))
				result.original = false;
		}
		for(int i=0;i<withinResults.size();i++){
			result = (search.WithinResult)withinResults.get(i);
			if(!result.original)
				System.out.println("\"" + result.sentence + "\" is flagged as plagiarised!");
		}	
	}
}
